package com.wans.user.aop;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wans.common.constans.CustomEnum;
import com.wans.user.common.em.UserRegisterEnum;
import com.wans.user.exception.UserRegisterException;
import com.wans.common.redis.RedisPrefix;
import com.wans.common.redis.RedisUtils;
import com.wans.model.user.dtos.UserDTO;
import com.wans.model.user.rdtos.RedisSession;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Aspect // 定义一个切面类
@Component
@Slf4j
@SuppressWarnings({"all"})
public class UserRegisterAspect {

    // 操作redis
    @Autowired
    private RedisUtils redisUtils;
    // 序列化类
    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 在controller执行成功后执行
     *
     * @param joinPoint 切入点的类信息
     */
    @AfterReturning(
            value = "execution(* com.wans.user.controller.v1.UserLoginController.login(..))"
    )
    public void UserRegisterSession(JoinPoint joinPoint) {

        // 获取 register 参数
        UserDTO userRegisterInfo = (UserDTO) joinPoint.getArgs()[0];

        // 返回前端的 UUID - value
        String cookieValue = UUID.randomUUID().toString().replaceAll("-", "");

        try {
            // 操作redis相关
            // 获取用户 id
            String userId = redisUtils.get(
                    RedisPrefix.USER_LOGIN_SUCCESS.getPrefix() + userRegisterInfo.getAccount()
            );
            // 删除id信息
            redisUtils.delete(RedisPrefix.USER_LOGIN_SUCCESS.getPrefix() + userRegisterInfo.getAccount());
            // 设置长cookie
            redisUtils.set(
                    RedisPrefix.USER_COOKIE_LONG_NAME_PREFIX.getPrefix() + cookieValue, userId,
                    Integer.parseInt(CustomEnum.REDIS_LONG_DAYS.getValue()), TimeUnit.DAYS
            );
            //TODO 多台设备登录限制 - 以后做

            // 设置短cookie信息
            RedisSession cookieValue2 = new RedisSession(
                    cookieValue,
                    System.currentTimeMillis() +
                            TimeUnit.HOURS.toMillis(Integer.parseInt(CustomEnum.REDIS_SHORT_HOURS.getValue()))
            );
            // 设置短cookie
            redisUtils.lPush(
                    RedisPrefix.USER_COOKIE_SHORT_NAME_PREFIX.getPrefix() + userId,
                    objectMapper.writeValueAsString(cookieValue2)
            );
        } catch (JsonProcessingException e) {
            throw new UserRegisterException(UserRegisterEnum.USER_JSON_OPTIONS_ERROR);
        } catch (RuntimeException e) {
            throw new UserRegisterException(UserRegisterEnum.USER_OTHER_ERROR);
        }

        // 登录成功 - 写入响应头 cookie
        HttpServletResponse response = (HttpServletResponse) joinPoint.getArgs()[1];
        response.addCookie(new Cookie(CustomEnum.LOGIN_COOKIE_NAME.getValue(), cookieValue));


    }

}


